package userInterface;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*;


import map.*;
//import player.*;


public class MainPanel extends JPanel implements MouseListener, KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 791401004216146080L;
	

	
	//Deklaration
	protected Map map;
	protected static Point2D mousePos;
	protected Cell position;
	
	
	//Konstruktor
	public MainPanel(int wid, int hig) {
		super();
		setSize(wid,hig);
		
		this.setDoubleBuffered(true);
		this.setFocusable(true);
		//this.requestFocus();
		
		map=new Map(40, 30);
		map.createGrid();
		POV.setZero();
		
		this.addMouseListener(this);
		super.addKeyListener(this);
	}
	
	
	//Run-Methode
	public void run() {
		while(true) {
			
		
			try {
				wait(2);
			} catch(Exception e) {}
		}
	}
	

	//Zeichnen-Methode
	public void paint(Graphics gfx) {
		super.paint(gfx);
		Graphics2D g = (Graphics2D)gfx;
	
		position = map.draw(g);
		
		
		g.drawString((POV.x/50.0)+" "+(POV.y/50.0), 20, 50);
		repaint();
	}
	
	
	//Implementierung der Interface Methode mousePos() von MousePosition
	public Point2D mousePos() {
		 try{
			 mousePos = new Point2D.Double((double)getMousePosition().x,(double)getMousePosition().y);
			 return mousePos;
		 }
		 catch(NullPointerException npe) {
			 return new Point2D.Double(0,0);
		 }
	}
	
	
	
	//"BAUEN"
	@Override
	public void mouseClicked(MouseEvent e) {	
		//Aufruf => Map
		map.build(position,Map.player1);
	}
	
	
	

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	//KeyListener Methoden

	@Override
	public void keyPressed(KeyEvent e) {
		
		int key = e.getKeyCode();
		switch(key) {
		 	case 37: 
		 		POV.left(map.getList());
		 		break;
		 	case 38:
		 		POV.up(map.getList());
		 		break;
		 	case 39:
		 		POV.right(map.getList());
		 		break;
		 	case 40:
		 		POV.down(map.getList());
		 		break;
		 			 	
		}
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}


}
